﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SpringGraph.ForceLayout;
using System.Collections.Generic;
using SpringGraph.Extensions;

namespace SpringGraph
{
    public class GraphEdge : AbstractEdge
    {
        public Dictionary<string, Node> TraversedMap = new Dictionary<string, Node>();

        public GraphEdge(GraphNode from, GraphNode to, int len)
            : base(from, to, len)
        {
        }

        public GraphNode ToNode
        {
            get
            {
                return To as GraphNode;
            }
        }

        public GraphNode FromNode
        {
            get
            {
                return From as GraphNode;
            }
        }

        public override double Length
        {
            get
            {
                double result = 50;

                if (ToNode != null && ToNode.View != null && FromNode != null && FromNode.View != null)
                {
                    var r = Math.Max(Math.Max(ToNode.View.GetRealWidth(), ToNode.View.GetRealHeight()),
                        Math.Max(FromNode.View.GetRealHeight(), FromNode.View.GetRealWidth()));

                    if (r > 0)
                        result = r;
                }

                return result;
            }
        }

        public bool Traversed
        {
            get
            {
                try
                {
                    var fromId = FromNode.Item.Id;
                    var toId = ToNode.Item.Id;
                    var key = fromId + "--" + toId;
                    return TraversedMap.ContainsKey(key);
                }
                catch (NullReferenceException)
                {
                    return false;
                }
            }
        }
    }
}
